package xyz.thoughtset.viewer.executor.blocks.executor;

import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Component;
import xyz.thoughtset.viewer.common.exc.exceptions.ExecException;
import xyz.thoughtset.viewer.executor.blocks.constants.NodeConstant;
import xyz.thoughtset.viewer.modules.step.entity.TaskBody;
import xyz.thoughtset.viewer.modules.step.entity.block.BlockBodyEle;
import xyz.thoughtset.viewer.modules.step.entity.block.BlockInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class TaskBlockExecutor extends AbstractBlockExecutor<TaskBody>  {


    @Override
    protected Object doQuery(BlockInfo block, TaskBody baseBlockBody, Map<String, Object> params, ExpressionParser parser, StandardEvaluationContext context) throws ExecException {
        // Implement the logic for executing a loop block here
        // This is a placeholder implementation
        List<Object> results = new ArrayList<>(baseBlockBody.listSize());

        context.setVariable("__ELENODE_", results);
        params.put("__ELENODE_", results);
//        int index = 0;
        for (BlockBodyEle ele : baseBlockBody.getBodyEles()) {
            results.add(execNode(ele, params, parser, context));
        }
        params.remove(NodeConstant.DATA_NODE);
        context.setVariable(NodeConstant.DATA_NODE,null);
        return results;
    }



}
